from django.contrib.auth.models import User, Group
from rest_framework import generics
from rest_framework.decorators import api_view
from rest_framework.reverse import reverse
from rest_framework.response import Response
from serializers import UserSerializer, GroupSerializer, FooSerializer, DeptsSerializer

from rest_framework import permissions
from rest_framework.permissions import IsAuthenticated
from rest_framework.authentication import SessionAuthentication, BasicAuthentication, TokenAuthentication
#from rest_framework.authentication import authentication_classes, permission_classes

from rest_framework.authtoken.models import Token

from rest_framework.views import APIView
from django.http import Http404
from rest_framework.response import Response
from rest_framework import status




from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import XMLRenderer
from rest_framework.parsers import JSONParser

#from snippets.serializers import SnippetSerializer




class JSONResponse(HttpResponse):
    """
    An HttpResponse that renders it's content into JSON.
    """
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)
        
        


@api_view(['GET'])
def api_root(request, format=None):
    """
    The entry endpoint of our API.
    """

    return Response({
        'users': reverse('user-list', request=request),
        'foo':reverse('foo', request=request)
        #'groups': reverse('group-list', request=request),
    })


'''
class DeptsList(generics.ListCreateAPIView ):
    #token = Token.objects.create(user=User.objects.all()[0])
    #user = User.objects.all()[0]
    #token = Token.objects.create(user=user)
    #print token

    #authentication_classes = (TokenAuthentication, BasicAuthentication)
    #authentication_classes = (SessionAuthentication, BasicAuthentication)
    #permission_classes = (IsAuthenticated,)


    from master.models import Depts    
    model = Depts
    serializer_class = DeptsSerializer
''' 
    
    
    
    
    
'''       
def DeptsList(request):
    """
    API endpoint that represents a list of users.
    """
    if request.method == 'GET':
        
        
        from master.models import Depts    
        
        depts = Depts.objects.all()
        serializer = DeptsSerializer(depts)
        
        return JSONResponse(serializer.data)
    
    else:
        return None
'''    

class DeptList(APIView):
    
    def get(self, request, format=None):
        from master.models import Depts
        depts = Depts.objects.all()
        serializer = DeptsSerializer(depts)
        return JSONResponse(serializer.data)
    
    def post(self, request, format=None):
        return None
    
    
    



class UserList(generics.ListCreateAPIView ):
    """
    API endpoint that represents a list of users.
    """
    #token = Token.objects.create(user=User.objects.all()[0])
    user = User.objects.all()[0]
    #token = Token.objects.create(user=user)
    #print token

    authentication_classes = (TokenAuthentication, BasicAuthentication)
    #authentication_classes = (SessionAuthentication, BasicAuthentication)
    permission_classes = (IsAuthenticated,)


    model = User
    serializer_class = UserSerializer

class UserDetail(generics.RetrieveUpdateDestroyAPIView):
    """
    API endpoint that represents a single user.
    """
    model = User
    serializer_class = UserSerializer

class GroupList(generics.ListCreateAPIView):
    """
    API endpoint that represents a list of groups.
    """
    model = Group
    serializer_class = GroupSerializer

class GroupDetail(generics.RetrieveUpdateDestroyAPIView):
    """
    API endpoint that represents a single group.
    """
    model = Group
    serializer_class = GroupSerializer
